package mbsl.commons.base;

import mbsl.ui.Tree;
import mbsl.util.SWTUtils;
import static mbsl.util.SWTUtils.Direction.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public abstract class ElementViewDialog {
	/**
	 * Opens the element view window.
	 */
	public void open() {
		mWindow.pack();
		
		mWindow.setVisible(true);
	}
	public CommentPanel getCommentPanel() {
		return mCommentPanel;
	}
	/**
	 * Creates a new dialog.
	 * @param pParentWindow The parent window to which the dialog will be modal.
	 * @param pTitle The title of the dialog window.
	 * @param pColor The colour theme of the dialog, described by RGB values in a size 3 integer array.
	 */
	protected ElementViewDialog(Shell pParentWindow, String pTitle, int[] pColor) {
		mHeaderColor = new Color(pParentWindow.getDisplay(), pColor[0], pColor[1], pColor[2]);
		mWindow = new Shell(pParentWindow, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		
		mWindow.setLayout(new FormLayout());
		
		if (pTitle == null)
			pTitle = "";
			
		mWindow.setText(pTitle);
		
		mWindow.setBackground(pParentWindow.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		mWindow.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		createHeading(pTitle);
		
		mCommentPanel = new CommentPanel(mWindow, pColor);

		mCommentPanel.setLayoutData(SWTUtils.getFormData(250, 250,
				SWTUtils.getMeta(down, 100, -MARGIN),
				SWTUtils.getMeta(left, 0, MARGIN),
				SWTUtils.getMeta(right, 100, -MARGIN)
		));
		
		createTreePanel();
		createInfoPanel();
	}
	/**
	 * Sets the dialog window title to pTitle
	 */
	protected void setTitle(String pTitle) {
		if (pTitle == null)
			pTitle = "";
		
		mWindow.setText(pTitle);
		mHeading.setText(pTitle.replaceAll("\\s&\\s", " && "));
		
		setHeadingSize();
	}
	/** 
	 * Adds a field to the information pane.  
	 * <p>
	 * The information will be organised in two columns, and are positioned as added, 
	 * left to right, then down. 
	 */
	protected Label addInfo() {
		Label info = new Label(mInfoPanel, SWT.LEAD | SWT.NO_BACKGROUND);

		info.setFont(mBold);
		info.setText(EMPTY);
		
		setInfoLayout(info);
		
		return info;
	}
	/** Sets the default layout data on the passed Label */
	private void setInfoLayout(Label pLabel) {
		pLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
	}
	/** Adds a Label with the passed text.  For use with the labels ("name:") */
	protected Label addLabel(String pText) {
		Label label = new Label(mInfoPanel, SWT.TRAIL | SWT.NO_BACKGROUND);
		
		label.setLayoutData(new GridData(SWT.END, SWT.BEGINNING, false, false));
		label.setText(pText);
		
		return label;
	}
	private void createTreePanel() {
		mTree = new Tree(mWindow, SWT.NONE);
		
		mTree.setLayoutData(SWTUtils.getFormData(
				SWTUtils.getMeta(up, mHeading, 0),
				SWTUtils.getMeta(left, 100, -320),
				SWTUtils.getMeta(right, 100, 0),
				SWTUtils.getMeta(down, 100, 0)
		));
	}
	/** Creates the info panel.  Must be called after creating the heading and comment folder. */
	private void createInfoPanel() {
		mInfoPanel = new Composite(mWindow, SWT.NONE);
		GridLayout layout = new GridLayout();
		
		layout.numColumns = 2;
		
		mInfoPanel.setLayout(layout);
		mInfoPanel.setLayoutData(SWTUtils.getFormData(
				SWTUtils.getMeta(up, mHeading, 0),
				SWTUtils.getMeta(left, 0, 0),
				SWTUtils.getMeta(right, mTree, 0),
				SWTUtils.getMeta(down, mCommentPanel.getComposite(), -5)
		));
	}
	/** Creates the heading panel */
	private void createHeading(String pText) {
		mHeading = new Label(mWindow, SWT.CENTER);
		
		mHeading.setText(pText.replaceAll("&", "&&"));
		mHeading.setFont(new Font(mWindow.getDisplay(), "Calibri", 40, SWT.BOLD));
		mHeading.setBackground(mHeaderColor);

		setHeadingSize();
	}
	private void setHeadingSize() {
		int width = mHeading.computeSize(SWT.DEFAULT, SWT.DEFAULT).x + 20;
		
		if (width < MINIMUM_WIDTH)
			width = MINIMUM_WIDTH;
		
		mHeading.setLayoutData(SWTUtils.getFormData(width, SWT.DEFAULT, 
				SWTUtils.getMeta(up, 0, 0),
				SWTUtils.getMeta(left, 0, 0),
				SWTUtils.getMeta(right, 100, 0)
		));
	}
	/**
	 * Returns the Shell of this dialog.
	 */
	public Shell getWindow() {
		return mWindow;
	}
	protected Composite getInfoPanel() {
		return mInfoPanel;
	}
	protected void pack() {
		mWindow.pack();
	}
	public Tree getTree() {
		return mTree;
	}
	
	private Tree mTree;
	private static Font mBold;
	private static final int MINIMUM_WIDTH = 600;
	private static final String EMPTY = "---";
	private static final int MARGIN = 2;
	private CommentPanel mCommentPanel;
	private Shell mWindow;
	private Color mHeaderColor;
	private Label mHeading;
	private Composite mInfoPanel;
}
